package fun.tan90.easy.binlog.manager.handler;

import cn.hutool.json.JSONUtil;
import com.mybatisflex.core.row.Db;
import fun.tan90.easy.binlog.base.constants.EasyBinlogConstants;
import fun.tan90.easy.binlog.base.model.RotateBinlogFile;
import fun.tan90.easy.binlog.base.strategy.AbstractExecuteStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * description TODO
 * company 铁人科技
 *
 * @author 陈浩杰
 * @date 2023/12/15 19:42
 */
@Slf4j
@Component
public class RotateBinlogFileHandler implements AbstractExecuteStrategy<String, Void> {

    @Override
    public String mark() {
        return EasyBinlogConstants.ROTATE_BINLOG_FILE;
    }

    @Override
    public void execute(String rotateBinlogFileJson) {
        RotateBinlogFile rotateBinlogFile = JSONUtil.toBean(rotateBinlogFileJson, RotateBinlogFile.class);
        Db.updateBySql("UPDATE binlog_master set filename = ?, position = ?, rotate_update_time = ? WHERE master_code = ?", rotateBinlogFile.getBinlogFilename(), rotateBinlogFile.getBinlogPosition(), new Date(), rotateBinlogFile.getMasterCode());
    }
}
